package com.xingxuecn.demos.controller;

import com.xingxuecn.demos.dao.domain.Role;
import com.xingxuecn.demos.service.RoleService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

/**
 * @author Kris at 2018/9/28 10:24
 */
@RestController
@RequestMapping("role")
public class RoleController {

    @Autowired
    RoleService roleService;

    @GetMapping({"index","","/"})
    public ModelAndView index(){
        return new ModelAndView("role");
    }

    /**
     * 查询角色
     * @return
     */
    @GetMapping("search")
    public List<Role> search(){
        return roleService.search();
    }

    /**
     * 详情 URL参数 PathVariable注解 {}站位
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public ResponseEntity<Role> detail(@PathVariable("id") int id){
        return ResponseEntity.ok(roleService.detail(id));
    }

    /**
     * RequestParam注解相当于request.getParameter()
     * RequestBody注解，从请求体拿参数
     * @param role
     * @return
     */
    @PostMapping
    public Role add(@RequestBody Role role, HttpServletRequest request){
        roleService.add(role);
        return role;
    }

    @PutMapping
    public Role update(@RequestBody Role role){
        roleService.update(role);
        return role;
    }


}
